package com.sci99.ex.vm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import com.sci99.ex.model.Category;
import com.sci99.ex.model.Chapter;
import com.sci99.ex.model.Course;
import com.sci99.ex.service.CategoryService;
import com.sci99.ex.service.ChapterService;
import com.sci99.ex.service.CourseService;

public class CourseVM {
	static Logger log = Logger.getLogger(CourseVM.class);
	private String keyword_name;
	private String keyword_code;
	private List<Course> list;
	private List<Category> cateList;
	private List<Chapter> chapList;
	private Course selected = new Course();
	private Category selectedCate = new Category();
	private Chapter selectedChap = new Chapter();

	public String getKeyword_name() {
		return keyword_name;
	}

	public void setKeyword_name(String keyword_name) {
		this.keyword_name = keyword_name;
	}

	public String getKeyword_code() {
		return keyword_code;
	}

	public void setKeyword_code(String keyword_code) {
		this.keyword_code = keyword_code;
	}

	public List<Course> getList() {
		return list;
	}

	public void setList(List<Course> list) {
		this.list = list;
	}

	public Course getSelected() {
		return selected;
	}

	public void setSelected(Course selected) {
		this.selected = selected;
	}

	public List<Category> getCateList() {
		return cateList;
	}

	public void setCateList(List<Category> cateList) {
		this.cateList = cateList;
	}

	public Category getSelectedCate() {
		return selectedCate;
	}

	public void setSelectedCate(Category selectedCate) {
		selected.setCategory_id(selectedCate.getId());
		this.selectedCate = selectedCate;
	}

	public Chapter getSelectedChap() {
		return selectedChap;
	}

	public void setSelectedChap(Chapter selectedChap) {
		this.selectedChap = selectedChap;
	}

	public List<Chapter> getChapList() {
		return chapList;
	}

	public void setChapList(List<Chapter> chapList) {
		this.chapList = chapList;
	}

	@AfterCompose
	public void doAfterCompose() {
		search();
		try {
			cateList = CategoryService.getCategoryActiveList();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	@Command
	@NotifyChange("list")
	public void search() {
		try {
			Map<String, Object> args = new HashMap<>(4, 1);
			if (keyword_code != null && keyword_code.trim().length() > 0)
				args.put("code", keyword_code.trim());
			else
				args.put("code", null);
			if (keyword_name != null && keyword_name.trim().length() > 0)
				args.put("name", keyword_name);
			else
				args.put("name", null);
			list = CourseService.getCourseList(args);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	@Command
	@NotifyChange({ "list", "chapList" })
	public void edit(@BindingParam("win") Window edWin) {
		try {
			chapList = ChapterService.getChapterList(selected);
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error(e.getMessage());
			return;
		}
		edWin.doModal();
	}

	@Command
	@NotifyChange("selected")
	public void add(@BindingParam("win") Window edWin) {
		selected = new Course();
		edWin.doModal();
	}

	@Command
	@NotifyChange("list")
	public void save(@BindingParam("win") Window edWin) {
		if (edWin != null)
			edWin.setVisible(false);
		try {
			CourseService.saveCourse(selected);
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error(e.getMessage());
			return;
		}
		Messagebox.show(Msg.OptOK);
		search();
	}

	@Command
	@NotifyChange("list")
	public void del(@BindingParam("win") Window edWin) {

		if (Messagebox.show(Msg.CfmDel, null,
				Messagebox.OK + Messagebox.CANCEL, Messagebox.QUESTION) != Messagebox.OK)
			return;
		if (selected.getId() != null) {
			try {
				CourseService.delCourse(selected);
			} catch (Exception e) {
				log.error(e.getMessage());
				return;
			}
		}
		edWin.setVisible(false);
		search();
	}

	@Command
	@NotifyChange("list")
	public void close(@BindingParam("win") Window edWin) {
		edWin.setVisible(false);
		search();
	}

	@Command
	@NotifyChange("selectedChap")
	public void addcp(@BindingParam("win") Window cpWin) {
		selectedChap = new Chapter();
		selectedChap.setCourse_id(selected.getId());
		cpWin.setVisible(true);
		cpWin.doModal();
	}

	@Command
	public void editcp(@BindingParam("win") Window cpWin){
		cpWin.setVisible(true);
		cpWin.doModal();
	}
	
	
	@Command
	@NotifyChange("chapList")
	public void delcp(@BindingParam("win") Window cpWin) {
		if (selectedChap == null || selectedChap.getId() == null) {
			Messagebox.show("请选择章节.");
			return;
		}

		if (Messagebox.show(Msg.CfmDel, null,
				Messagebox.OK + Messagebox.CANCEL, Messagebox.QUESTION) != Messagebox.OK)
			return;
		try {
			ChapterService.delChapter(selectedChap);
			chapList = ChapterService.getChapterList(selected);
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error("", e);
			return;
		}
		Messagebox.show(Msg.OptOK);
		cpWin.setVisible(false);
	}

	@Command
	@NotifyChange("selected")
	public void closecp(@BindingParam("win") Window cpWin) {
		cpWin.setVisible(false);
	}

	@Command
	@NotifyChange("chapList")
	public void savecp(@BindingParam("win") Window cpWin) {
		try {
			ChapterService.saveChapter(selectedChap);
			chapList = ChapterService.getChapterList(selected);
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error(e.getMessage());
			return;
		}
		Messagebox.show(Msg.OptOK);
		cpWin.setVisible(false);
	}
}
